home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 79 / maccd 79.iso / multimedial / GL Tron / Source / gltron / directory.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-05-20  |  4.1 KB  |  182 lines  |  [TEXT/CWIE]

  1. #include <stdio.h>
  2. #include "gltron.h"
  3.  
  4. #ifdef macintosh
  5. static char* subdir = ":Data";
  6. #else 
  7. static char* subdir = "data";
  8. #endif
  9.  
  10. /* the following code doesn't compile under non-POSIX systems (they
  11.    don't have dirent.h), so we do lots of special casing */
  12.  
  13. #ifdef macintosh
  14.  
  15. /* macintosh code by Darrell Walisser */
  16.  
  17. #include <IterateDirectory.h>
  18. #include <FullPath.h>
  19. #include <Types.h>
  20. #include <Files.h>
  21. #include <Processes.h>
  22.  
  23. /* -dw- From Technical Q&A FL14 */
  24. /* http://developer.apple.com/qa/fl/fl14.html */
  25.  
  26.  /* GetApplicationDirectory returns the volume reference number
  27.   and directory ID for the current application's directory. */
  28.      
  29.  OSStatus GetApplicationDirectory(short *vRefNum, long *dirID) {
  30.      ProcessSerialNumber PSN;
  31.      ProcessInfoRec pinfo;
  32.      FSSpec pspec;
  33.      OSStatus err;
  34.          /* valid parameters */
  35.      if (vRefNum == NULL || dirID == NULL) return paramErr;
  36.          /* set up process serial number */
  37.      PSN.highLongOfPSN = 0;
  38.      PSN.lowLongOfPSN = kCurrentProcess;
  39.          /* set up info block */
  40.      pinfo.processInfoLength = sizeof(pinfo);
  41.      pinfo.processName = NULL;
  42.      pinfo.processAppSpec = &pspec;
  43.          /* grab the vrefnum and directory */
  44.      err = GetProcessInformation(&PSN, &pinfo);
  45.      if (err == noErr) {
  46.          *vRefNum = pspec.vRefNum;
  47.          *dirID = pspec.parID;
  48.      }
  49.      return err;
  50.  }
  51.  
  52. pascal void iterateProc (const CInfoPBRec * const cpb_ptr,
  53.                                   Boolean *quit_flag,
  54.                                   void *user_data) {
  55.     char *filename;                              
  56.     int   len;
  57.  
  58.     list *l = (list*) user_data;
  59.  
  60.     if ( ((cpb_ptr->hFileInfo.ioFlFndrInfo.fdFlags) & kIsInvisible ) == 0) {    
  61.    
  62.            while (l->next != NULL)
  63.               l = l->next;
  64.  
  65.         len = cpb_ptr->hFileInfo.ioNamePtr[0];
  66.         cpb_ptr->hFileInfo.ioNamePtr[len+1] = '\0';
  67.         filename = cpb_ptr->hFileInfo.ioNamePtr + 1;
  68.  
  69.         l->data = malloc (sizeof(char) * len + 1);                                
  70.         strcpy (l->data, filename);
  71.  
  72.         l->next = (list*) malloc (sizeof (list));
  73.         l->next->next = NULL;
  74.         l->next->data = NULL;
  75.     }
  76. }
  77.  
  78. list* readDirectoryContents(char *dirname, char *prefix) {
  79.  
  80.   list *l;
  81.   OSErr err;
  82.   char *path;
  83.   
  84.   Str255    relPath;
  85.   StringPtr pstr = relPath; /* Str255 is an array, but I want pointer arithmetic */
  86.   
  87.   short vRefNum;
  88.   long  dirID;
  89.   FSSpec spec;
  90.   
  91.   int len = strlen(dirname) + 1;
  92.   
  93.   pstr[0] = len;
  94.   
  95.   if (*dirname != ':') { /* we only handle relative paths, so make it so */
  96.     pstr[0]++;
  97.     pstr[1]  = ':';
  98.     pstr++;
  99.   }
  100.  
  101.   memcpy (pstr + 1, dirname, len);
  102.   pstr [ len ] = ':';
  103.  
  104.   err = GetApplicationDirectory (&vRefNum, &dirID);
  105.   if (err != noErr) {
  106.     fprintf (stderr, "GetApplicationDirectory failed\n");
  107.     exit (-1);
  108.   }
  109.   
  110.   err = FSMakeFSSpec  (vRefNum, dirID, relPath, &spec);
  111.      
  112.   if (err != noErr) {
  113.     fprintf (stderr, "FSMakeFSSpec failed\n");
  114.     exit (-1);
  115.   }
  116.   
  117.   l = (list*) malloc(sizeof(list));
  118.   l->data = NULL;
  119.   l->next = NULL;
  120.  
  121.   err = FSpIterateDirectory (&spec, 1, iterateProc, l);
  122.     
  123.   if (err != noErr)  {
  124.     fprintf (stderr, "FSpIterateDirectory failed\n");
  125.     exit (-1);
  126.   }
  127.  
  128.   return l;
  129. }
  130.  
  131. char *getMusicPath(char *name) {
  132.   char *path;
  133.   path = malloc(strlen(subdir) + 1 + strlen(name) + 1);
  134.   sprintf(path, "%s%c%s", subdir, SEPERATOR, name);
  135.   return path;
  136. }
  137.  
  138. /* end of macintosh code */
  139.  
  140. #else
  141.  
  142. #include <sys/types.h>
  143. #include <dirent.h>
  144.  
  145. list* readDirectoryContents(char *dirname, char *prefix) {
  146.   DIR *dir;
  147.   struct dirent *entry;
  148.   list *l, *p;
  149.  
  150.   l = (list*) malloc(sizeof(list));
  151.   p = l;
  152.   p->next = NULL;
  153.  
  154.   dir = opendir(dirname);
  155.   while((entry = readdir(dir)) != NULL) {
  156.     char *name;
  157.     if(prefix == NULL || strstr(entry->d_name, prefix) == entry->d_name) {
  158.       if(entry->d_name[0] != '.') {
  159.     name = malloc(strlen(entry->d_name) + 1);
  160.     memcpy(name, entry->d_name, strlen(entry->d_name) + 1);
  161.     p->data = name;
  162.     p->next = (list*) malloc(sizeof(list));
  163.     p = p->next;
  164.     p->next = NULL;
  165.       }
  166.     }
  167.   }
  168.   closedir(dir);
  169.   return l;
  170.  
  171. }
  172.  
  173. char *getMusicPath(char *name) {
  174.   char *path;
  175.   path = malloc(strlen(subdir) + 1 + strlen(name) + 1);
  176.   sprintf(path, "%s%c%s", subdir, SEPERATOR, name);
  177.   return path;
  178. }
  179.  
  180. #endif
  181.  
  182.